<template>
  <!-- 弹窗区域 -->
  <div>
    <el-dialog title="选择我的兴趣技能" width="900px" v-model="props.dialogVisible">
      <el-input
        class="search"
        placeholder="搜索兴趣技能"
        v-model="searchInput"
        @blur="change"
      />
      <el-button
        class="search-btn"
        type="primary"
        @click="search"
        :disabled="searchInput? false :true"
      >搜索
      </el-button>
      <el-form label-position="left" label-width="auto" class="mt-60">
        <el-tag
          class="tag"
          closable
          :disable-transitions="false"
          v-for="item in hobby"
          :key="item.id"
          @click="deleteHandler(item.id)">
          {{ item }}
        </el-tag>
      </el-form>
      <div class="text">推荐兴趣技能
        <span class="span">&nbsp;&nbsp;&nbsp;( {{ hobby.length }}/7 )</span>
      </div>
      <el-form>
        <div class="hobbylist">
          <el-checkbox-group :max="7" v-model="hobby">
            <el-checkbox
              size="large"
              class="checkbox"
              v-for="item in hobbyList"
              :key="item.id"
              border :label="item.name"
              @click="addHandler($event)"
            />
          </el-checkbox-group>
        </div>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" size="large" color="#f93684" @click="save">确认</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import {defineComponent} from "vue";

export default defineComponent({
  name: "Popup",
})
</script>

<script setup lang="ts">
import {onMounted, ref,} from "vue";
import {reqInterestList} from "@/api/profile";
//获取pinia数据

// 选中的兴趣
const hobby = ref<any>([])
// 兴趣列表
const hobbyList = ref<any>([])
// 保存搜索参数
const searchInput = ref<any>("")
//初始化请求
onMounted(() => {
    getHobby()
  }
)
// 父子传参
const props = defineProps<{
  dialogVisible: Boolean
}>()
const emits = defineEmits<{
  (e:'delOp'):any
}>()
// 删除兴趣
const deleteHandler = (e: any) => {
  hobby.value.splice(hobby.value.indexOf(e), 1)
}
// 添加兴趣
const addHandler = (e: any) => {
  // 是否包含?
  if (e.target.value && hobby.value.includes(e.target.value)) {
    // 点击删除包含的
    hobby.value.splice(hobby.value.indexOf(e.target.value), 1)
    return
  }
  // 如果不包含，则添加
  if (e.target.value && !hobby.value.includes(e.target.value)) {
    hobby.value.push(e.target.value)
  }
}
//获取兴趣列表
const getHobby = async () => {
  try {
    const res = await reqInterestList();
    hobbyList.value = res.data.content
  } catch (e) {
    return Promise.reject(e)
  }
}
// 搜索
const search = () => {
  hobbyList.value = hobbyList.value.filter((item: { name: string | any[]; }) => item.name.includes(searchInput.value))
}
// 失焦重新获取请求
const change = () => {
  if (searchInput.value?.trim() === "") {
    getHobby()
  }
}
//点击确定的回调
const save = ()=>{
  console.log(1)
  emits('delOp')
}
</script>

<style lang="scss" scoped>

.search {
  width: 300px;
  height: 40px;
  position: absolute;
  left: 30px;
  border-radius: 4px;
}

.search-btn {
  width: 70px;
  height: 40px;
  position: absolute;
  left: 340px;
}

.tag {
  width: 100px;
  height: 40px;
  text-align: center;
  margin-left: 10px;
  font-size: 15px;
  color: black;
  background-color: #fff;
  border: solid 0.5px #eee;
}

.mt-60 {
  margin-top: 60px;
  text-align: left;
}

.text {
  text-align: left;
  font-size: 16px;
  margin-top: 20px;
  color: black;
}

.span {
  color: #aaa;
}

.hobbylist {
  text-align: left;
  margin-top: 15px;
  margin-left: 10px;
}

.checkbox {
  width: 110px;
  margin-top: 10px;
}
</style>
